const CloudDBZoneWrapper = require("./CloudDBZoneWrapper.js");

let myHandler = async function (event, context, callback, logger) {
  /*logger.info(event);

  // do something here

  callback({
    code: 0,
    desc: "Success."
  });*/


  var action;
  var data;

  const cloudDBZoneWrapper = new CloudDBZoneWrapper(logger);


  if (event.body) {
    var _body = JSON.parse(event.body);

    action = _body.action;
    data = _body.extraData;
  } else {
    action = event.action;
    data = event.extraData;
  }


  logger.info("data: " + JSON.stringify(data))
  let queryResult;
  switch (action) {
    case 'all':

      queryResult = await cloudDBZoneWrapper.queryAll();
      break;

    case 'compound':

      queryResult = await cloudDBZoneWrapper.queryCompound(data);
      break;

    case 'average':
      queryResult = await cloudDBZoneWrapper.queryAverage(data);
      // logger.info(queryResult);
      console.log(queryResult);
      break;

    case 'sum':
      queryResult = await cloudDBZoneWrapper.querySum(data);
      // logger.info(queryResult);
      console.log(queryResult);
      break;

    case 'max':
      queryResult = await cloudDBZoneWrapper.queryMax(data);
      // logger.info(queryResult);
      console.log(queryResult);
      break;

    case 'min':
      queryResult = await cloudDBZoneWrapper.queryMin(data);
      // logger.info(queryResult);
      console.log(queryResult);
      break;

    case 'count':
      queryResult = await cloudDBZoneWrapper.queryCount(data);
      // logger.info(queryResult);
      console.log(queryResult);
      break;

    case 'orderasc':
      queryResult = await cloudDBZoneWrapper.orderByAsc(data);
      console.log(queryResult);
      break;

    case 'orderdesc':
      queryResult = await cloudDBZoneWrapper.orderByDesc(data);
      console.log(queryResult);
      break;

    default:
      logger.info("invalid action");
      console.log("invalid action");

  }

  callback(queryResult);

};

export { myHandler };